package edu.uwosh.cs342.group1;

import java.util.ArrayList;

import edu.uwosh.cs342.group1.database.DbCloudHelper;
import edu.uwosh.cs342.group1.storage.MultipleChoice;
import edu.uwosh.cs342.group1.storage.Question;
import edu.uwosh.cs342.group1.storage.Quiz;
import edu.uwosh.cs342.group1.storage.Scores;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MultipleChoiceDisplay extends Activity {

	RadioButton answer1,answer2,answer3,answer4;
	RadioGroup answers;
	TextView question;
	ArrayList<Question> questions;
	int correct;
	int total;
	Question currentQuestion;
	DbCloudHelper db;
	int quizID;
	String quizName, username;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		db = new DbCloudHelper();

		setContentView(R.layout.multiple_choice);
		correct = this.getIntent().getIntExtra("correct", 0);
		total = this.getIntent().getIntExtra("total",0);
		questions = this.getIntent().getParcelableArrayListExtra("questions");
		quizID = this.getIntent().getIntExtra("QuizId",0);
		quizName = this.getIntent().getStringExtra("quizName");
		username = this.getIntent().getStringExtra("username");

		currentQuestion = questions.get(total);

		question = (TextView)findViewById(R.id.question);
		answer1 = (RadioButton)findViewById(R.id.answer1);
		answer2 = (RadioButton)findViewById(R.id.answer2);
		answer3 = (RadioButton)findViewById(R.id.answer3);
		answer4 = (RadioButton)findViewById(R.id.answer4);

		answers = (RadioGroup)findViewById(R.id.answers);

		question.setText("Question " + (total+1)+ " of " + questions.size() + ": " +currentQuestion.getTitle());
		answer1.setText(((MultipleChoice) currentQuestion).getAnswer(0));
		answer2.setText(((MultipleChoice) currentQuestion).getAnswer(1));
		answer3.setText(((MultipleChoice) currentQuestion).getAnswer(2));
		answer4.setText(((MultipleChoice) currentQuestion).getAnswer(3));

	}

	public void submit(View v) {
		String answer = "I SHOULD CHANGE";
		int buttonID = answers.getCheckedRadioButtonId();
		View button = answers.findViewById(buttonID);
		System.out.println(answers.indexOfChild(button));
		switch(answers.indexOfChild(button)) {
		case 0:
			answer = (String) answer1.getText();
			break;
		case 1:
			answer = (String) answer2.getText();
			break;
		case 2:
			answer = (String) answer3.getText();
			break;
		case 3:
			answer = (String) answer4.getText();
			break;
		default:
			answer = "";
		}
		System.out.println("Answer is " +answer);
		if(answer.compareTo("")!=0){
			if(answer.compareToIgnoreCase(currentQuestion.getKey())==0) {
				correct++;
			}
		}
		total++;
		if(questions.size()>total) {
			Question q = questions.get(total);
			if(q.isMultipleChoice()) {
				Intent i = new Intent(MultipleChoiceDisplay.this,
						MultipleChoiceDisplay.class);
				i.putExtra("questions", questions);
				i.putExtra("correct", correct);
				i.putExtra("total", total);
				i.putExtra("quizName", quizName);
				i.putExtra("QuizId",quizID);
				i.putExtra("username",username);
				MultipleChoiceDisplay.this.startActivity(i);
				System.out.println("MultipleChoice next!");
			}else if(q.isTrueFalse()){
				Intent i = new Intent(MultipleChoiceDisplay.this,
						TrueFalseDisplay.class);
				i.putExtra("questions", questions);
				i.putExtra("correct", correct);
				i.putExtra("total", total);
				i.putExtra("quizName", quizName);
				i.putExtra("QuizId",quizID);
				i.putExtra("username",username);
				
				MultipleChoiceDisplay.this.startActivity(i);
				System.out.println("TrueFalse next!");
			} else if(q.isShortAnswer()) {
				Intent i = new Intent(MultipleChoiceDisplay.this,
						ShortAnswerDisplay.class);
				i.putExtra("questions", questions);
				i.putExtra("correct", correct);
				i.putExtra("total", total);
				i.putExtra("quizName", quizName);
				i.putExtra("QuizId",quizID);
				i.putExtra("username",username);
				MultipleChoiceDisplay.this.startActivity(i);
				System.out.println("ShortAnswer nextt!");
			}
			finish();
		}else {
			AlertDialog.Builder alert = new AlertDialog.Builder(this);
			
			alert.setTitle("Submit Score?");
			alert.setMessage("Would you like to submit your score?\nCorrect: " +
			correct + "\nTotal: "+total);
			
			alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int whichButton) {
					// TODO Auto-generated method stub
					Scores score = new Scores();
					Quiz quiz = new Quiz();
					quiz.setId(quizID);
					quiz.setName(quizName);
					score.setUsername(username);
					score.setQuiz(quiz);
					score.setScore(Scores.calculateScore(correct, total));
					score.setIsPublic(1);
					db.SubmitScore(score);
					Intent i = new Intent(MultipleChoiceDisplay.this,QuizAppActivity.class);
					//startActivity(i);
					finishActivity(1);
					finish();
				}
			});
			
			alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Scores score = new Scores();
					Quiz quiz = new Quiz();
					quiz.setId(quizID);
					quiz.setName(quizName);
					score.setUsername(username);
					score.setQuiz(quiz);
					score.setScore(Scores.calculateScore(correct, total));
					score.setIsPublic(0);
					db.SubmitScore(score);
					Intent i = new Intent(MultipleChoiceDisplay.this,QuizAppActivity.class);
					//startActivity(i);
					finishActivity(1);
					finish();
				}
			});
			
			alert.show();
		}
	}

}
